package com.marketingexpert.controller.marketingmgnt.activityrecomment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class GoItemDiscountController implements Controller {

	private String dispatchUrl;
	
	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf8");
		
		String[] itemIds = request.getParameterValues("itemId");
		
		
		
		String pTitles = request.getParameter("ptitles");
		String[] titles = new String[itemIds.length];
		
		if(itemIds.length > 1) {
			titles = pTitles.split(",");
		} else {
			titles[0] = pTitles;
		}
		
		List<Map> _res = new ArrayList<Map>();
		
		for(int i=0; i<itemIds.length; i++) {
			Map<String, String> results = new HashMap<String, String>();
			results.put("itemId", itemIds[i]);
			results.put("title", titles[i]);
			request.getSession().removeAttribute("rec_" + itemIds[i]);
			_res.add(results);
		}
		
		request.setAttribute("pageNo", request.getParameter("pageNo"));
		request.setAttribute("forwordUrl", request.getParameter("forwordUrl"));
		return new ModelAndView(getDispatchUrl(),"results", _res);
	}

	public String getDispatchUrl() {
		return dispatchUrl;
	}

	public void setDispatchUrl(String dispatchUrl) {
		this.dispatchUrl = dispatchUrl;
	}
}
